package org.apache.elasticjob;

import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
import org.apache.shardingsphere.elasticjob.dataflow.props.DataflowJobProperties;
import org.apache.shardingsphere.elasticjob.http.props.HttpJobProperties;
import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.ScheduleJobBootstrap;
import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
import org.apache.shardingsphere.elasticjob.script.props.ScriptJobProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class Test {
	@Autowired
	private CoordinatorRegistryCenter coordinatorRegistryCenter;
	@Bean
	public void testSimpleJob() {
        new ScheduleJobBootstrap(coordinatorRegistryCenter, new MySimpleJob(), createJobConfiguration1()).schedule();
	}
	
	@Bean
	public void testDataflowJob() {
        new ScheduleJobBootstrap(coordinatorRegistryCenter, new MyDataflowJob(), createJobConfiguration2()).schedule();
	}
	
	@Bean
	public void testScriptJob() {
		new ScheduleJobBootstrap(coordinatorRegistryCenter, "SCRIPT", createJobConfiguration3()).schedule();
	}
	@Bean
	public void testHttpJob() {
		new ScheduleJobBootstrap(coordinatorRegistryCenter, "HTTP", createJobConfiguration4()).schedule();
	}
	
	private static JobConfiguration createJobConfiguration1() {
        return JobConfiguration.newBuilder("MySimpleJob-1", 1)
                .cron("0/8 * * * * ?")
                .build();
    }
	
    private static JobConfiguration createJobConfiguration2() {
        return JobConfiguration.newBuilder("MyDataflowJob-1", 1)
                .cron("0/9 * * * * ?")
                .build();
    }
    
    /**
     *  注意ScheduleJobBootstrap的第二个参数为"SCRIPT"，另外通过设置script.command.line来配置要执行的脚本。
     * 其底层其实就是利用的CommandLine来执行的命令，所以只要在你机器上能执行的命令，那么就可以在这里进行设置并执行。
     */
    private static JobConfiguration createJobConfiguration3() {
        return JobConfiguration.newBuilder("MyScriptJob", 1)
                .cron("0/12 * * * * ?")
                .setProperty(ScriptJobProperties.SCRIPT_KEY, "java -version")
                .overwrite(true)
                .build();
    }
    
    private static JobConfiguration createJobConfiguration4() {
        // 创建作业配置
        return JobConfiguration.newBuilder("MyHttpJob", 1)
                .cron("0/13 * * * * ?")
                .setProperty(HttpJobProperties.URI_KEY, "http://www.baidu.com")
                .setProperty(HttpJobProperties.METHOD_KEY, "GET")
                .setProperty(HttpJobProperties.DATA_KEY, "source=ejob") // 请求体
                .overwrite(true)
                .build();

    }
    
    /**
     * 1.记录日志：只记录日志，不中断作业执行，默认就是这种策略
2.抛出异常：抛出系统异常，并中断作业执行
3.忽略异常：忽略系统异常，不中断作业执行
4.邮件通知策略
5.企业微信通知策略
6.钉钉通知策略
     * @return
     */
    @SuppressWarnings("unused")
	private static JobConfiguration createJobConfiguration() {
    	return JobConfiguration.newBuilder("MySimpleJob", 1)
    		.cron("0/5 * * * * ?")
    		//                .jobErrorHandlerType("LOG")     // 记录日志
    		//                .jobErrorHandlerType("THROW")   // 抛出异常
    		//                .jobErrorHandlerType("IGNORE")  // 忽略异常
    		.overwrite(true)
    		.build();

    }
    
    /**
     * 如果开启流式处理，则作业只有在 fetchData 方法的返回值为 null 或集合容量为空时，才停止抓取，否则作业将一直运行下去；
     *  如果关闭流式处理，则作业只会在每次作业执行过程中执行一次 fetchData 和 processData 方法，随即完成本次作业。
     * @return
     */
    @SuppressWarnings("unused")
	private static JobConfiguration createJobConfiguration99() {
    	return JobConfiguration.newBuilder("MyDataflowJob", 1)
    		.cron("0/3 * * * * ?")
    		//streaming.process=true，表示开启流式处理，默认为false
    		.setProperty(DataflowJobProperties.STREAM_PROCESS_KEY, "true")
    		//overwrite=true，表示要重写Job配置，如果不设置这个，新修改的或新增的配置将不会生效
    		.overwrite(true)
    		.build();
    }
    
}
